﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KabiManagementWeb.Models;
using System.Globalization;
namespace KabiManagementWeb.Controllers
{
    public class StockController : Controller
    {
        KBManagementDataContext context = new KBManagementDataContext();
        StatisticsFacade facade = new StatisticsFacade(DBUtils.DBType.Local);
        //
        // GET: /Stock/

        public ActionResult Index()
        {

            return View(context.Stores);
        }

        //
        // GET: /Stock/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Stock/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Stock/Create

        [HttpPost]
        public ActionResult Create(Store collection)
        {
            try
            {
                if (collection.Quantity >= collection.QuantityStandard)
                {
                    collection.warning = "Đủ hàng";
                }
                else
                {
                    collection.warning = "Thiếu hàng";
                }
                context.Stores.InsertOnSubmit(collection);
                context.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Stock/Edit/5

        public ActionResult Edit(string id)
        {
            
            return View(context.Stores.First(x => x.ID.Equals(id)));
        }

        //
        // POST: /Stock/Edit/5

        [HttpPost]
        public ActionResult Edit(string id, Store collection)
        {
            try
            {
             var st=  context.Stores.First(x => x.ID.Equals(id));
                st.ID=collection.ID;
                st.S_Name=collection.S_Name;
                st.Quantity=collection.Quantity;
                st.QuantityStandard=collection.QuantityStandard;
                st.Unit=collection.Unit;
                 if (collection.Quantity >= collection.QuantityStandard)
                {
                    st.warning = "Đủ hàng";
                }
                else
                {
                    st.warning = "Thiếu hàng";
                }
                context.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Stock/Delete/5

        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Stock/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        public ActionResult Stock()
        {
            GetViewData();
            return View(facade.GetStoreLog());
        }
        private void GetViewData()
        {
            ViewData["drop"] = new SelectList(new List<SelectListItem> { 
                new SelectListItem { Text = "Nhập Kho", Value = "true" },
                new SelectListItem { Text = "Xuat Kho", Value = "false" } }, "Value", "Text");
            ViewData["Time"] = new SelectList(new List<SelectListItem> { 
                new SelectListItem { Text = "Trong ngày", Value = "day" },
                new SelectListItem { Text = "Trong Tuần", Value = "week" } ,
                new SelectListItem { Text = "Trong Tháng", Value = "month" }   }, "Value", "Text");
        }
        [HttpPost]
        public ActionResult Stock(string time, bool type)
        {
            bool b = Convert.ToBoolean(type);
            IEnumerable<Store_Log> list = null;
            if (time == "day")
            {
                list = facade.GetStoreLogByType(b).Where(x => x.TransferDate.Value.Day == DateTime.Now.Day);
            }
            else if (time == "week")
            {

                CultureInfo ciCurr = CultureInfo.CurrentCulture;

                list = facade.GetStoreLogByType(b).Where(x => ciCurr.Calendar.GetWeekOfYear(x.TransferDate.Value, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday) == ciCurr.Calendar.GetWeekOfYear(DateTime.Now, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday));
            }
            else
            {
                list = facade.GetStoreLogByType(b).Where(x => x.TransferDate.Value.Month == DateTime.Now.Month);
            }
            GetViewData();
            return View(list);
        }
        public ActionResult TempStock()
        {
            return View(context.StoreTemps);
        }
    }
}
